package org.demo.rmi.data;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 1. RMI 中实现类必须继承 UnicastRemoteObject
 * 2. RMI 中实现类的每个方法必须声明抛出 RemoteException (在接口中声明过的可不再声明)
 * @author  
 * @date    2010-11-9
 * @file    org.demo.rmi.data.DbAdapterImpl.java
 */
public class DbAdapterImpl extends UnicastRemoteObject implements DbAdapter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long sn = -1;
	/**
	 * 构造函数
	 * @throws RemoteException
	 */
	public DbAdapterImpl() throws RemoteException {
	}
	/**
	 * 获取下一个最大值
	 * @return
	 */
	@Override
	public long getNextValue() throws RemoteException{
		if (sn == -1){
			// sn = getMaxValueFromDatabase();
			sn = 0;
		}
		// print server info
		String server = this.ref.remoteToString();
		String regex = "endpoint:\\[(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\:(\\d{1,})\\]";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(server);
		if (m.find()){
			System.out.println("server ip : " + m.group(1));
			System.out.println("server data port : " + m.group(2));
		}
		try {
			System.out.println("client ip : " + DbAdapterImpl.getClientHost());			
		} catch (Exception e) {
			e.printStackTrace();
		}
		// end print
		return ++sn;
	}
}
